package com.bbex.mapper;

import com.bbex.entity.PpMoveCoinVolume;
import com.bbex.sql.build.PpMoveCoinVolumeBuild;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PpMoveCoinVolumeDao {

    @InsertProvider(type = PpMoveCoinVolumeBuild.class, method = "insert")
    void insert(PpMoveCoinVolume coinVolume);

    @Select("SELECT " + PpMoveCoinVolumeBuild.columns + " FROM pp_move_coin_volume t WHERE t.coin_symbol = #{coinSymbol} AND t.user_id = #{userId} AND t.volume > 0 ORDER BY t.create_date ASC ")
    List<PpMoveCoinVolume> findByCoinSymbol(@Param("coinSymbol") String coinSymbol, @Param("userId") String userId);

    @Select("SELECT t.coin_symbol, MIN(t.coin_id) AS coin_id, SUM(t.volume) AS volume FROM pp_move_coin_volume t WHERE t.user_id = #{userId} AND t.volume > 0 GROUP BY t.coin_symbol ")
    List<PpMoveCoinVolume> findCoinFund(@Param("userId") String userId);

    @Select("SELECT t.coin_symbol, SUM(t.volume) AS volume FROM pp_move_coin_volume t WHERE t.user_id = #{userId} AND t.volume > 0 AND t.coin_symbol = #{coinSymbol} GROUP BY t.coin_symbol ")
    PpMoveCoinVolume findFundByCoinSymbol(@Param("userId") String userId, @Param("coinSymbol") String coinSymbol);

    @Update("UPDATE pp_move_coin_volume SET volume = #{volume}, break_volume = #{breakVolume}, version = version + 1 WHERE id = #{id} AND version = #{version} ")
    long updateVolume(PpMoveCoinVolume coinVolume);
}
